:root {
  /* Dark Theme (Default) */
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-hover: #334155;
  --border: #334155;
  --text: #f1f5f9;
  --text-secondary: #94a3b8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #ef4444;
  --success: #22c55e;
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --radius: 12px;
  --font-sans: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

[data-theme="light"] {
  --bg: #f8fafc;
  --panel: #ffffff;
  --panel-hover: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-secondary: #64748b;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #ef4444;
  --success: #22c55e;
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.container {
  width: 100vw;
  height: 100vh;
  display: flex;
  overflow: hidden;
  background: var(--bg);
}

.layout {
  flex: 1;
  display: flex;
  width: 100%;
  height: 100%;
  max-width: 1600px;
  margin: 0 auto;
}

/* Sidebar */
.sidebar {
  width: 280px;
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px;
  flex-shrink: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  padding: 0 8px;
}

.sidebar-header strong {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text);
}

.topics {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.topics li {
  padding: 10px 12px;
  margin-bottom: 4px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--text-secondary);
  transition: all 0.2s ease;
}

.topics li:hover {
  background: var(--panel);
  color: var(--text);
}

.topics li.active {
  background: var(--panel-hover);
  color: var(--text);
  font-weight: 500;
}

.topic-name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.delete-topic-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  opacity: 0;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.topics li:hover .delete-topic-btn {
  opacity: 1;
}

.delete-topic-btn:hover {
  background: var(--danger);
  color: white;
}

/* Main Chat Area */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  position: relative;
  min-width: 0;
  /* Fix flex child overflow */
}

.header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(15, 23, 42, 0.8);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
  background: linear-gradient(to right, #60a5fa, #a78bfa);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.toolbar {
  display: flex;
  gap: 12px;
}

.btn {
  padding: 8px 16px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn:hover {
  background: var(--panel-hover);
  border-color: var(--text-secondary);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.btn.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn.small {
  padding: 4px 10px;
  font-size: 0.75rem;
}

/* Messages */
.messages {
  flex: 1;
  list-style: none;
  margin: 0;
  padding: 24px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.messages li {
  display: flex;
  flex-direction: column;
  max-width: 80%;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.messages li.me {
  align-self: flex-end;
  align-items: flex-end;
}

.messages li.system {
  align-self: center;
  max-width: 600px;
  text-align: center;
}

.meta {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-bottom: 4px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.delete-message-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 1rem;
  padding: 0;
  opacity: 0;
  transition: opacity 0.2s;
}

.messages li:hover .delete-message-btn {
  opacity: 1;
}

.delete-message-btn:hover {
  color: var(--danger);
}

.bubble {
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 0.95rem;
  line-height: 1.6;
  position: relative;
  word-wrap: break-word;
}

.messages li:not(.me):not(.system) .bubble {
  background: var(--panel);
  border-top-left-radius: 4px;
}

.messages li.me .bubble {
  background: var(--accent);
  color: white;
  border-top-right-radius: 4px;
}

.messages li.system .bubble {
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.875rem;
  padding: 8px;
  border: 1px dashed var(--border);
}

/* Markdown Styles within Bubble */
.bubble p {
  margin: 0 0 1em 0;
}

.bubble p:last-child {
  margin: 0;
}

.bubble pre {
  background: rgba(0, 0, 0, 0.3);
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
}

.bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85em;
  background: rgba(0, 0, 0, 0.2);
  padding: 2px 4px;
  border-radius: 4px;
}

.bubble pre code {
  background: transparent;
  padding: 0;
}

.bubble a {
  color: inherit;
  text-decoration: underline;
}

/* Input Form */
.form {
  padding: 24px;
  background: var(--bg);
  border-top: 1px solid var(--border);
  display: flex;
  gap: 12px;
  position: relative;
}

.form input {
  flex: 1;
  padding: 14px 18px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font-size: 1rem;
  outline: none;
  transition: all 0.2s;
  box-shadow: var(--shadow);
}

.form input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.form button {
  padding: 0 24px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: var(--shadow);
}

.form button:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.form button:active {
  transform: translateY(0);
}

.form button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

/* Loading Indicator */
.loading-indicator {
  position: absolute;
  bottom: 90px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  padding: 8px 16px;
  border-radius: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  font-size: 0.875rem;
  color: var(--text-secondary);
  z-index: 5;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.hint {
  text-align: center;
  padding: 8px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  opacity: 0.6;
}

/* Modals */
.modal {
  display: none;
  position: fixed;
  z-index: 100;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  animation: fadeIn 0.2s ease;
}

.modal-content {
  background: var(--panel);
  margin: 8vh auto;
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 90%;
  max-width: 800px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal-header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.25rem;
}

.close {
  color: var(--text-secondary);
  font-size: 1.5rem;
  cursor: pointer;
  transition: color 0.2s;
}

.close:hover {
  color: var(--text);
}

.modal-body {
  padding: 24px;
  overflow-y: auto;
}

.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* Status Grid */
.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
}

.status-card {
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
}

.status-card h3 {
  margin: 0 0 12px 0;
  font-size: 1rem;
  color: var(--text-secondary);
}

.status-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 0.9rem;
}

/* Process Table */
.process-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.process-table th {
  text-align: left;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  color: var(--text-secondary);
}

.process-table td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

.process-table tr:last-child td {
  border-bottom: none;
}

/* Responsive */
@media (max-width: 768px) {
  .layout {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    height: auto;
    max-height: 200px;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .header {
    padding: 12px 16px;
  }

  .header h1 {
    font-size: 1rem;
  }

  .toolbar .btn {
    padding: 6px 12px;
    font-size: 0.8rem;
  }

  .messages {
    padding: 16px;
  }

  .form {
    padding: 16px;
  }
}

/* Copy & Selection Styles */
.copy-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  padding: 2px;
  opacity: 0;
  transition: opacity 0.2s;
}

.messages li:hover .copy-btn {
  opacity: 1;
}

.copy-btn:hover {
  transform: scale(1.1);
}

.msg-checkbox {
  margin-right: 12px;
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: var(--accent);
}

.messages li {
  display: flex;
  flex-direction: row;
  /* Changed to row to accommodate checkbox */
  align-items: flex-start;
  gap: 8px;
}

/* Adjust bubble and meta for row layout */
.messages li .meta {
  order: 1;
  margin-bottom: 0;
  margin-left: 4px;
  white-space: nowrap;
}

.messages li .bubble {
  order: 2;
  flex: 1;
}

/* Revert flex direction for 'me' messages if not in selection mode? 
   Actually, keeping it consistent is better, but 'me' messages usually align right.
   Let's adjust for 'me' class.
*/
.messages li.me {
  flex-direction: row-reverse;
}

.messages li.me .meta {
  margin-left: 0;
  margin-right: 4px;
}

/* When in selection mode, we might want checkboxes on the left always */
.messages li:has(.msg-checkbox) {
  flex-direction: row;
}

.messages li:has(.msg-checkbox).me {
  flex-direction: row;
  /* Force left alignment or keep checkbox on left? */
}

/* Let's keep it simple: Checkbox always first child */
.messages li .msg-checkbox {
  order: 0;
  margin-top: 12px;
  /* Align with top of bubble roughly */
}

/* Toast Notification */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 0.9rem;
  opacity: 0;
  transition: all 0.3s ease;
  z-index: 2000;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.divider {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 8px;
}